package cn.lanqiao.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import cn.lanqiao.entity.ForgetWord;
import cn.lanqiao.entity.RememberWord;
import cn.lanqiao.service.WordService;
import cn.lanqiao.service.impl.WordServiceImpl;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;

public class TranslationFrame extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextPane translationText;
	private MainFrame mainFrame = new MainFrame();
	private ForgetWord fw = new ForgetWord();
	private WordService ws = new WordServiceImpl();
	private RememberWord rw = new RememberWord();
	private String word;
	private String translation;
	private JTable table;
	
	public static void main(String[] args) {
		try {
			TranslationFrame dialog = new TranslationFrame();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TranslationFrame(JTable table) {
		this();
		this.table = table;
		this.word = table.getValueAt(MainFrame.i, 0).toString();
		this.translation = table.getValueAt(MainFrame.i, 1).toString();
		this.translationText.setText(translation);
	}
	public TranslationFrame() {
		setResizable(false);
		setModal(true);
		setAlwaysOnTop(true);
		setBounds(100, 100, 577, 331);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(SystemColor.scrollbar);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblMean = new JLabel("mean");
		lblMean.setForeground(new Color(0, 191, 255));
		lblMean.setFont(new Font("方正舒体", Font.PLAIN, 36));
		lblMean.setBounds(236, 95, 98, 48);
		contentPanel.add(lblMean);
		
		translationText = new JTextPane();
		translationText.setBounds(122, 153, 323, 55);
		contentPanel.add(translationText);
		
		JButton btnRemember = new JButton("记得");
		btnRemember.setForeground(new Color(0, 0, 102));
		btnRemember.setBackground(new Color(255, 204, 204));
		btnRemember.setFont(new Font("方正舒体", Font.PLAIN, 16));
		btnRemember.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						rw = getRememberWord();
						ws.saveRememberWord(rw);
						openWindow();//打开背单词窗口
			}
		});
		btnRemember.setBounds(56, 243, 93, 23);
		contentPanel.add(btnRemember);
		
		JButton button_1 = new JButton("模糊");
		button_1.setForeground(new Color(0, 0, 102));
		button_1.setBackground(new Color(255, 204, 204));
		button_1.setFont(new Font("方正舒体", Font.PLAIN, 16));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						fw = getForgetWord();
						ws.saveForgetWord(fw);
						openWindow();//打开背单词窗口
			}
		});
		button_1.setBounds(240, 243, 93, 23);
		contentPanel.add(button_1);
		
		JButton button_2 = new JButton("不记得");
		button_2.setForeground(new Color(0, 0, 102));
		button_2.setBackground(new Color(255, 204, 204));
		button_2.setFont(new Font("方正舒体", Font.PLAIN, 16));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						fw = getForgetWord();
						ws.saveForgetWord(fw);
						openWindow();//打开背单词窗口
			}
		});
		button_2.setBounds(421, 243, 93, 23);
		contentPanel.add(button_2);
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon("photoes\\translation.png"));
		label_1.setBounds(0, 0, 571, 302);
		contentPanel.add(label_1);
		
		this.setLocationRelativeTo(null);
	}
	
	//打开背单词窗口
	public void openWindow() {
		MainFrame.i++;
		if(MainFrame.i >= mainFrame.getLength()) {
			//mainFrame.setVisible(true);
			TranslationFrame.this.dispose();
			JOptionPane.showMessageDialog(null, "恭喜复习完成");
			return;
		}
		WordFrame wordFrame = new WordFrame(table);
		TranslationFrame.this.dispose();
		wordFrame.setVisible(true);
	}
	//获取记得的单词信息以及账号
	public RememberWord getRememberWord() {
		int wordId = ws.getWordId(word);
		String account = UserLoginFrm.account;
		rw.setWord(word);
		rw.setTranslation(translation);
		rw.setWordId(wordId);
		rw.setAccount(account);
		return rw;
	}
	//获取不记得的单词信息以及账号
	public ForgetWord getForgetWord() {
		int wordId = ws.getWordId(word);
		String account = UserLoginFrm.account;
		fw.setWord(word);
		fw.setTranslation(translation);
		fw.setWordId(wordId);
		fw.setAccount(account);
		return fw;
	}
}
